<?PHP
/*
 * $Id: PathWildcardTest.class.php 49 2009-05-16 02:20:14Z namba@craid.co.jp $
 * =============================================================================
 * Barge - Web application framework on PHP5 
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\utils;

use \Exception;
use \UnitTestCase;

use barge\utils\ArrayList;
use barge\utils\HashMap;
use barge\utils\IndexOutOfBoundsException;
use barge\utils\IllegalAccessException;
use barge\utils\PathWildcard;

/**
 * @see UnitTestCase
 */
class PathWildcardTest extends UnitTestCase 
{
	/**
	 * @test
	 */
	public function testMatch() 
	{
		$wildcard = new PathWildcard('*.html');
		$this->assertTrue($wildcard->match('index.html') === true);
		$this->assertTrue($wildcard->match('.html') === true);
		$this->assertTrue($wildcard->match('index.php.html') === true);
		$this->assertTrue($wildcard->match('src/index.php.html') === true);
		$this->assertTrue($wildcard->match('src/a/index.html') === true);
		$this->assertTrue($wildcard->match('src/index.php') === false);
		$this->assertTrue($wildcard->match('src\index.php') === false);
		$this->assertTrue($wildcard->match('index.php') === false);
		$this->assertTrue($wildcard->match('*.html') === true);
		$this->assertTrue($wildcard->match('index(1).html') === true);
		$this->assertTrue($wildcard->match('index[1].html') === true);
		$this->assertTrue($wildcard->match('_index.html') === true);
		
		$wildcard = new PathWildcard('a_*.html');
		$this->assertTrue($wildcard->match('index.html') === false);
		$this->assertTrue($wildcard->match('a_index.html') === true);
		$this->assertTrue($wildcard->match('a_.html') === true);
		$this->assertTrue($wildcard->match('a_0929.html') === true);
		$this->assertTrue($wildcard->match('a_/index.html') === false);
		$this->assertTrue($wildcard->match('a_sample.php.html') === true);
		
		$wildcard = new PathWildcard('a?.html');
		$this->assertTrue($wildcard->match('ab.html') === true);
		$this->assertTrue($wildcard->match('abc.html') === false);
		$this->assertTrue($wildcard->match('a/.html') === false);
		$this->assertTrue($wildcard->match('a?.html') === true);
		$this->assertTrue($wildcard->match('a*.html') === true);
		
		$wildcard = new PathWildcard('src/*.html');
		$this->assertTrue($wildcard->match('src/a.html') === true);
		$this->assertTrue($wildcard->match('src/a.php') === false);
		$this->assertTrue($wildcard->match('a/a.html') === false);
		$this->assertTrue($wildcard->match('src/.html') === true);
		$this->assertTrue($wildcard->match('a.html') === false);
		$this->assertTrue($wildcard->match('src/a/b.html') === false);
		
		$wildcard = new PathWildcard('src/**/*.html');
		$this->assertTrue($wildcard->match('src/a.html') === false);
		$this->assertTrue($wildcard->match('src/a.php') === false);
		$this->assertTrue($wildcard->match('src/a/b.html') === true);
		$this->assertTrue($wildcard->match('src/a/b.php') === false);
		$this->assertTrue($wildcard->match('src/a/b/c.html') === true);
		$this->assertTrue($wildcard->match('src/a/b/c.php') === false);
		
		$wildcard = new PathWildcard('**/index.html');
		$this->assertTrue($wildcard->match('src/index.html') === true);
		$this->assertTrue($wildcard->match('src/a.php') === false);
		$this->assertTrue($wildcard->match('src/a/index.html') === true);
		$this->assertTrue($wildcard->match('src/a/b/index.html') === true);
		$this->assertTrue($wildcard->match('src/a/b/c.php') === false);

		$wildcard = new PathWildcard(':');
		$this->assertTrue($wildcard->match('index.html') === false);
		
		$wildcard = new PathWildcard('*');
		$this->assertTrue($wildcard->match('index.html') === true);
		
		$wildcard = new PathWildcard('index.html');
		$this->assertTrue($wildcard->match('index.html') === true);
		$this->assertTrue($wildcard->match('index.php') === false);

		$wildcard = new PathWildcard('src/index.html');
		$this->assertTrue($wildcard->match('src\index.html') === true);


	}

	/**
	 * @test
	 */
	public function testMatchRoot() 
	{
		$wildcard = new PathWildcard('*.html');
		$this->assertTrue($wildcard->matchRoot('index.html') === true);
		$this->assertTrue($wildcard->matchRoot('index.htm') === false);
		
		$wildcard = new PathWildcard('src/*.html');
		$this->assertTrue($wildcard->matchRoot('src/index.html') === true);
		$this->assertTrue($wildcard->matchRoot('classes/') === null);
		
		$wildcard = new PathWildcard('/2004/*/*.gif');
		$this->assertTrue($wildcard->matchRoot('/2004/img/sample.gif') === true);
		
		$wildcard = new PathWildcard('/style/**/*.htm');
		$this->assertTrue($wildcard->matchRoot('/style/') === false);
		$this->assertTrue($wildcard->matchRoot('/style') === false);
		$this->assertTrue($wildcard->matchRoot('/js') === null);
		$this->assertTrue($wildcard->matchRoot('/style/a.htm') === false);
		$this->assertTrue($wildcard->matchRoot('/style/2004/a.htm') === true);
		$this->assertTrue($wildcard->matchRoot('/style/2004/c/a.htm') === true);
	}
	
	/**
	 * @test
	 */
	public function testReplace() 
	{
		$wildcard = new PathWildcard('src/*.html');
		$this->assertTrue($wildcard->replace('src/index.html', 'src/_$1.html') === 'src/_index.html');
		$this->assertTrue($wildcard->replace('index.html', 'src/_$1.html') === null);
		
		$wildcard = new PathWildcard('src/**/*.html');
		$this->assertTrue($wildcard->replace('src/test/sample.html', 'src/$1/$2.phtml') === 'src/test/sample.phtml');
		
		$wildcard = new PathWildcard('*.html');
		$this->assertTrue($wildcard->replace('src/test/sample.html', 'src/test/$1.html.back') === 'src/test/sample.html.back');

	}
}

?>